﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EmgrMvc.Models;
using EmgrMvc.BLL;
using Microsoft.AspNetCore.Mvc.Rendering;
using CsvHelper;
using System.IO;
using System.Globalization;

namespace EmgrMvc.Controllers
{
    public class HomeController : Controller
    {
        private readonly MyContext _context;
        MqttTool tool;

        public HomeController(MyContext context)
        {
            _context = context;
            tool = MqttTool.getSingleton(); // new MqttTool();
        }

        public IActionResult Index()
        {
            return View();
            //return RedirectToAction("Hello");
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Hello()
        {
            //ViewBag.Mac = "A1:B2:C3:D4:E5:F6";
            return Redirect("WakeupPC");
        }

        [HttpPost]
        public IActionResult Hello(string Action, string mac)
        {
            //WakeupPC(mac);

            ViewBag.Action = Action;
            ViewBag.Mac = mac;
            return View();
        }

        [HttpGet]
        public IActionResult WakeupPC()
        {
            List<MqttController> controllers = GetMqttControllers();
            ViewBag.ControllerId = new SelectList(controllers, "ID", "Name");

            WakeupInfo wakeupInfo = new WakeupInfo();
            wakeupInfo.ControllerId = "405";
            wakeupInfo.Mac = "a1:b2:c3:d4:e5:f6";
            return View();
        }

        [HttpPost]
        public IActionResult WakeupPC(WakeupInfo wakeupInfo)
        {
            //MqttTool tool = new MqttTool(_context);
            tool.WakeupPC(wakeupInfo.ControllerId + "/wol", wakeupInfo.Mac);

            List<MqttController> controllers = GetMqttControllers();
            ViewBag.ControllerId = new SelectList(controllers, "ID", "Name");
            return View(wakeupInfo);
        }

        public IActionResult ACControl()
        {
            //ViewBag.topic = "hw/home/wol";
            //List<MqttController> controllers = new List<MqttController>();
            ////controllers.Add(new MqttController { ID = "home", Name = "家里的" });
            //controllers.Add(new MqttController { ID = "home", Name = "home" });
            //controllers.Add(new MqttController { ID = "seig/zh/405", Name = "405" });
            //controllers.Add(new MqttController { ID = "seig/zh/410", Name = "410" });

            List<MqttController> controllers = GetMqttControllers();
            ViewBag.ControllerId = new SelectList(controllers, "ID", "Name");
            return View();
        }

        private static List<MqttController> GetMqttControllers()
        {
            using (CsvReader csvReader = new CsvReader(new StreamReader("Data/controllers.csv"), CultureInfo.CurrentCulture))
            {
                List<MqttController> controllers = csvReader.GetRecords<MqttController>().ToList();
                return controllers;
            }
        }

        [HttpPost]
        public IActionResult ACControl(ACControlInfo acControlInfo)
        {
            //MqttTool tool = new MqttTool(_context);
            if (acControlInfo.Action == "ACPowerOn")
                tool.ACControl(acControlInfo.ControllerId + "/ir/" + acControlInfo.ACNumber, "ON");
            else if (acControlInfo.Action == "ACPowerOff")
                tool.ACControl(acControlInfo.ControllerId + "/ir/" + acControlInfo.ACNumber, "OFF");

            List<MqttController> controllers = GetMqttControllers();
            ViewBag.ControllerId = new SelectList(controllers, "ID", "Name");
            return View();
        }

        public IActionResult Test()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Test(TestInfo info)
        {
            //MqttTool tool = new MqttTool(_context);
            if (info.Action == "ACPowerOn")
                tool.ACControl(info.Topic, info.Data);
            else if (info.Action == "ACPowerOff")
                tool.ACControl(info.Topic, info.Data);
            else if (info.Action == "WakeupPC")
                tool.WakeupPC(info.Topic, info.Data);

            return View();
        }



        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
